package com.wicket.okrapp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.wicket.okrapp.base.service.MOmsEvaCycleConfService;
import com.wicket.okrapp.base.service.dto.req.QueryCycleConfDetailReq;
import com.wicket.okrapp.base.service.dto.req.QueryCycleConfListReq;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.common.util.CommonFunctionHelper;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaCycleConfMapper;
import com.wicket.okrapp.dal.po.mbg.OmsEvaCycleConf;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.ArrayList;
import com.wicket.okrapp.base.service.dto.req.BatchQueryCycleConfReq;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MOmsEvaCycleConfServiceImpl
        extends ServiceImpl<OmsEvaCycleConfMapper, OmsEvaCycleConf>
        implements MOmsEvaCycleConfService {

    @Autowired
    OmsEvaCycleConfMapper omsEvaCycleConfMapper;

    /**
   * 3-2-08查周期配置列表[2539]
   * gen by moon at 10/14/2022, 9:26:29 AM
   */
 @Trace(operationName = "3-2-08查周期配置列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsEvaCycleConf>  queryCycleConfList(QueryCycleConfListReq reqDto) {
     
        try {
            QueryWrapper<OmsEvaCycleConf> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaCycleConf.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaCycleConf.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaCycleConf.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaCycleConf>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 3-2-08新增周期配置[2537]
   * gen by moon at 10/14/2022, 9:26:27 AM
   */
 @Trace(operationName = "3-2-08新增周期配置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addCycleConf(OmsEvaCycleConf omsEvaCycleConf) {
     omsEvaCycleConf.setCycleConfId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
omsEvaCycleConf.setIsArchive("FALSE");
omsEvaCycleConf.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsEvaCycleConf.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsEvaCycleConf.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsEvaCycleConf.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsEvaCycleConf.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsEvaCycleConf.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsEvaCycleConf.setIsValid("FALSE");
omsEvaCycleConf.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
omsEvaCycleConf.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(omsEvaCycleConf)){
        return omsEvaCycleConf.getCycleConfId();
     }else {
        return "-1";
     }
    
  }

    /**
   * 3-2-08查周期配置详情[2360]
   * gen by moon at 10/14/2022, 9:26:26 AM
   */
 @Trace(operationName = "3-2-08查周期配置详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  OmsEvaCycleConf  queryCycleConfDetail(QueryCycleConfDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<OmsEvaCycleConf> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaCycleConf> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                OmsEvaCycleConf retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 3-2-08修改周期配置[2538]
   * gen by moon at 10/14/2022, 9:26:28 AM
   */
 @Trace(operationName = "3-2-08修改周期配置")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateCycleConf(OmsEvaCycleConf omsEvaCycleConf) {
     omsEvaCycleConf.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsEvaCycleConf.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
omsEvaCycleConf.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
omsEvaCycleConf.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(omsEvaCycleConf!=null && omsEvaCycleConf.getSubjectLifeCycle()!=null && omsEvaCycleConf.getSubjectLifeCycle().equals("ARCHIVING")) {
              omsEvaCycleConf.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
        return super.updateById(omsEvaCycleConf);
  }

    /**
   * 3-2-03批量增周期配置快照[2433]
   * gen by moon at 10/14/2022, 9:26:27 AM
   */
 @Trace(operationName = "3-2-03批量增周期配置快照")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  batchAddCycleConfSnapshot(List<OmsEvaCycleConf> omsEvaCycleConf) {
     
      if(omsEvaCycleConf==null || omsEvaCycleConf.size()==0){
        return false;
      }
      
      return super.saveBatch(omsEvaCycleConf.stream().map(item->{
       item.setCycleConfId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
item.setSubjectLifeCycle("EDITING");
item.setIsArchive("FALSE");
item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
item.setIsValid("FALSE");
item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
          return item;
      }).collect(Collectors.toList()));
  }
/**
   * 3-2-08批量查询周期配置by周期类型[6408]
   * gen by moon at 5/1/2023, 10:03:41 PM
   */
 @Trace(operationName = "3-2-08批量查询周期配置by周期类型")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<OmsEvaCycleConf>  batchQueryCycleConf(BatchQueryCycleConfReq reqDto) {

        try {
            QueryWrapper<OmsEvaCycleConf> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if(reqDto.getCycleConfList()==null || reqDto.getCycleConfList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("cycle_type_code", reqDto.getCycleConfList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaCycleConf.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaCycleConf.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaCycleConf.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaCycleConf>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }
}
